package com.example.labSystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.labSystem.entity.Weekly;
import com.example.labSystem.mapper.WeeklyMapper;
import com.example.labSystem.service.IWeeklyService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 周报表 服务实现类
 * </p>
 *
 * @author 10
 * @since 2024-12-12
 */
@Service
public class WeeklyServiceImpl extends ServiceImpl<WeeklyMapper, Weekly> implements IWeeklyService {

    /**
     * 查询所有周报，结果缓存到 Redis
     * 缓存的 key 是 "allReports"
     */
    @Override
    @Cacheable(value = "weeklyReports", key = "'allReports'")
    public List<Weekly> listAllReports() {
        return list();
    }

    /**
     * 根据用户名查询周报
     * 缓存的 key 是 "username_{username}"
     */
    @Override
    @Cacheable(value = "weeklyReports", key = "'username_' + #username")
    public List<Weekly> getReportsByUsername(String username) {
        return lambdaQuery()
                .eq(Weekly::getUsername, username)
                .list();
    }

    /**
     * 根据周报标题查询周报，结果缓存到 Redis
     * 缓存的 key 是 "reportTitle_{reportTitle}"
     */
    @Override
    @Cacheable(value = "weeklyReports", key = "'reportTitle_' + #reportTitle")
    public List<Weekly> searchByTitle(String reportTitle) {
        return lambdaQuery()
                .like(Weekly::getReportTitle, reportTitle)
                .list();
    }

    /**
     * 新增周报，保存数据并清除缓存
     */
    @Override
    @CacheEvict(value = "weeklyReports", allEntries = true)
    public boolean save(Weekly weekly) {
        return super.save(weekly);
    }

    /**
     * 更新周报，更新数据并清除缓存
     */
    @Override
    @CacheEvict(value = "weeklyReports", allEntries = true)
    public boolean updateById(Weekly weekly) {
        return super.updateById(weekly);
    }

    /**
     * 根据周报ID删除周报，并清除缓存
     * 清除所有相关缓存，如 "allReports" 和 "reportID_{reportID}"
     */
    @Override
    @CacheEvict(value = "weeklyReports", allEntries = true)
    public boolean deleteById(String reportID) {
        return removeById(reportID);
    }

}
